<div class="tmd-doc">
<h1 class="tmd-header-1">
Keywords and Identifiers in Go
</h1>
<p></p>
<div class="tmd-usual">
This article will introduce keywords and identifiers in Go.
</div>
<p></p>
<h3 id="keyword" class="tmd-header-3">
Keywords
</h3>
<p></p>
<div class="tmd-usual">
Keywords are the special words which help compilers understand and parse user code.
</div>
<p></p>
<div class="tmd-usual">
Up to now (Go 1.25), Go has only 25 keywords:
</div>
<p></p>
<pre class="tmd-code line-numbers fixed-width">
<code class="language-go">break     default      func    interface  select
case      defer        go      map        struct
chan      else         goto    package    switch
const     fallthrough  if      range      type
continue  for          import  return     var
</code></pre>
<p></p>
<div class="tmd-usual">
They can be categorized as four groups:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">const</code>, <code class="tmd-code-span">func</code>, <code class="tmd-code-span">import</code>, <code class="tmd-code-span">package</code>, <code class="tmd-code-span">type</code> and <code class="tmd-code-span">var</code> are used to declare all kinds of code elements in Go programs.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">chan</code>, <code class="tmd-code-span">interface</code>, <code class="tmd-code-span">map</code> and <code class="tmd-code-span">struct</code> are used as parts in some composite type denotations.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">break</code>, <code class="tmd-code-span">case</code>, <code class="tmd-code-span">continue</code>, <code class="tmd-code-span">default</code>, <code class="tmd-code-span">else</code>, <code class="tmd-code-span">fallthrough</code>, <code class="tmd-code-span">for</code>, <code class="tmd-code-span">goto</code>, <code class="tmd-code-span">if</code>, <code class="tmd-code-span">range</code>, <code class="tmd-code-span">return</code>, <code class="tmd-code-span">select</code> and <code class="tmd-code-span">switch</code> are used to control flow of code.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">defer</code> and <code class="tmd-code-span">go</code> are also control flow keywords, but in other specific manners. They modify function calls, which we'll talk about in <a href="control-flows-more.html">this article</a>.
</div>
</li>
</ul>
<p></p>
<p></p>
<div class="tmd-usual">
These keywords will be explained in details in other articles.
</div>
<p></p>
<h3 id="identifier" class="tmd-header-3">
Identifiers
</h3>
<p></p>
<div class="tmd-usual">
An identifier is a token which must be composed of Unicode letters, Unicode digits and <code class="tmd-code-span">_</code> (underscore), and start with either an Unicode letter or <code class="tmd-code-span">_</code>. Here,
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
Unicode letters mean the characters defined in the Letter categories <span class="tmd-italic">Lu</span>, <span class="tmd-italic">Ll</span>, <span class="tmd-italic">Lt</span>, <span class="tmd-italic">Lm</span>, or <span class="tmd-italic">Lo</span>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
Unicode digits mean the characters defined in the Number category <span class="tmd-italic">Nd</span> of The Unicode Standard.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
<span class="tmd-bold">keywords</span> can not be used as identifiers.
</div>
<p></p>
<div class="tmd-usual">
Identifier <code class="tmd-code-span">_</code> is a special identifier, it is called <span class="tmd-bold">blank identifier</span>.
</div>
<p></p>
<div class="tmd-usual">
Later we will learn that all names of types, variables, constants, labels, package names and package import names must be identifiers.
</div>
<p></p>
<div class="tmd-usual">
An identifier starting with an <a href="http://www.fileformat.info/info/unicode/category/Lu/list.htm">Unicode upper case letter</a> is called an <span class="tmd-bold">exported identifier</span>. The word <span class="tmd-bold"><span class="tmd-italic">exported</span></span> can be interpreted as <span class="tmd-bold"><span class="tmd-italic">public</span></span> in many other languages. The identifiers which don't start with an Unicode upper case letter are called non-exported identifiers. The word <span class="tmd-bold"><span class="tmd-italic">non-exported</span></span> can be interpreted as <span class="tmd-bold"><span class="tmd-italic">private</span></span> in many other languages. Currently (Go 1.25), eastern characters are viewed as non-exported letters. Sometimes, non-exported identifiers are also called unexported identifiers.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
Here are some legal exported identifiers:
</div>
<pre class="tmd-code line-numbers fixed-width">
<code class="language-go">Player_9
DoSomething
VERSION
Ĝo
Π
</code></pre>
<p></p>
<div class="tmd-usual">
Here are some legal non-exported identifiers:
</div>
<pre class="tmd-code line-numbers fixed-width">
<code class="language-go">_
_status
memStat
book
π
一个类型
변수
エラー
</code></pre>
<p></p>
<div class="tmd-usual">
And here are some tokens which are illegal to be used as identifiers:
</div>
<pre class="tmd-code line-numbers fixed-width">
<code class="language-go">// Starting with a Unicode digit.
123
3apples

// Containing Unicode characters not
// satisfying the requirements.
a.b
*ptr
$name
a@b.c

// These are keywords.
type
range
</code></pre>
<p></p>
</div>
